﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Invoicing.Model.Orders;
using Invoicing.Logic.Orders;
using Invoicing.Logic.Financial;
using System.IO;
using Utils;
using System.Text;
namespace Invoicing.Web.Management.Areas.Orders.Controllers
{
    [RouteArea("Orders")]
    [RoutePrefix("BookOrder")]
    [Authorize]
    public class BookOrderController : Controller
    {
        [Route("")]
        public ActionResult Index()
        {
            return View();
        }

        [Route("NewOrder/{method:int}")]
        public ActionResult NewOrder(int method,string orderNo=null)
        {
            ViewBag.Method = method;
            ViewBag.OrderNo = orderNo;
            return View();
        }

        [Route("Order/{orderNo}")]
        public ActionResult Order(string orderNo)
        {
            BookOrderModel model = null;
            if (!string.IsNullOrEmpty(orderNo))
                model = BookOrderLogic.GetOrder(orderNo);
            return PartialView(model);
        }

        [Route("Print/{orderNo}")]
        public ActionResult Print(string orderNo)
        {
           BookOrderModel model = null;
           if (!string.IsNullOrEmpty(orderNo))
           {
               model = BookOrderLogic.GetOrder(orderNo);
               if (model != null)
               {
                   return View(model);
               }
           }
            throw new HttpException(404, "无法找到该单据");
        }

        [Route("Export")]
        public ActionResult Export()
        {
            try
            {
                BookOrderQuery sea = new BookOrderQuery();
                sea.GetCustomer = true;
                sea.GetItems = true;
                sea.GetProduct = true;
                if (!string.IsNullOrEmpty(Request.Form.Get("customerID")))
                    sea.CustomerID = int.Parse(Request.Form.Get("customerID"));
                sea.OrderNo = Request.Form.Get("orderNo");
                var orderStatus = Request.Form.Get("orderStatus");
                if (!string.IsNullOrEmpty(orderStatus))
                    sea.OrderStatus = orderStatus.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToArray();
                var odb = Request.Form.Get("orderDateBefore");
                if (!string.IsNullOrEmpty(odb))
                    sea.OrderDateBefore = DateTime.Parse(odb);
                var oda = Request.Form.Get("orderDateAfter");
                if (!string.IsNullOrEmpty(oda))
                    sea.OrderDateAfter = DateTime.Parse(oda);
                var orderMethod = Request.Form.Get("orderMethod");
                if (!string.IsNullOrEmpty(orderMethod))
                    sea.Method = orderMethod.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToArray();
                var lst = BookOrderLogic.GetOrders(sea);
                if (lst.Count > 0)
                {
                    string txt = "单据号\t客户\t日期\t单据类型\t产品\t材料\t加工\t规格\t数量(只)\t单据状态";
                    foreach (var o in lst)
                    {
                        foreach (var it in o.Items)
                        {
                            txt += string.Format("\r\n{0}\t{1}\t{2:yyyy-MM-dd}\t{3:0.00}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}", new object[] {
                            o.OrderNo,o.Customer.ComplexName,o.NoteDate,Common.GetOrderName(o.Method),
                            it.Product.Module,it.Material.MaterialName,it.Process.ProcessName,it.Size,
                            it.Quantity,Common.GetOrderStatus(o.OrderStatus)
                            });
                        }
                    }
                    var buffer = Encoding.UTF8.GetBytes(txt);
                    return File(buffer, "application/octet-stream", "订单详表.txt");
                }
                else
                    return Content("暂无数据");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }
    }
}
